package com.mycompany.tpfinal.abm;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;


public class PeliculaDAOImpl implements PeliculaDAO {

    //Atributos
    private Conexion conexion;

    //Constructor
    public PeliculaDAOImpl() {
        conexion =  new Conexion();
    }
       

    //Métodos
    public String generaAlta(Pelicula pelicula) throws SQLException, IOException {
        
        String resultado = null;
        final String query = "INSERT INTO peliculas (NAME, DESCRIPTION,GENRE_ID,DIRECTOR,MAIN_ACTORS,DURATION) VALUES(?,?,?,?,?,?)";

        try {
            Connection cn = conexion.obtieneConexion();
            PreparedStatement ps = cn.prepareStatement(query);
            ps.setString(1, pelicula.getNombre());
            ps.setString(2, pelicula.getDescripcion());
            ps.setString(3, pelicula.getGenero());
            ps.setString(4, pelicula.getDirector());
            ps.setString(5, pelicula.getActorPrincipal());
            ps.setInt(6, pelicula.getDuracion());
            ps.executeUpdate();            
            ps.close();
            cn.close();
        } catch (SQLException e) {
            resultado = e.getMessage();
        }
        return resultado;
    }
 

    public String generaBaja(Integer id) throws SQLException, IOException {
        String resultado = null;
        final String queryBaja = "DELETE FROM peliculas WHERE ID = ?";
        try {
            Connection cn = conexion.obtieneConexion();
            PreparedStatement ps=cn.prepareStatement(queryBaja);
            ps.setInt(1, id);
            ps.executeUpdate();
            
            cn.close();
            ps.close();
        } catch (SQLException e) {
            resultado=e.getMessage();
        }
        return resultado;
    }

    public String generaModificacion(Pelicula pelicula) throws SQLException, IOException {
        
        String resultado  = null;
        final String queryModificacion= "UPDATE peliculas SET NAME = ?, DESCRIPTION = ?,GENRE_ID = ?, DIRECTOR = ?,MAIN_ACTORS = ?, DURATION = ? WHERE ID = ?";
             try {
            Connection cn=conexion.obtieneConexion();
            PreparedStatement ps = cn.prepareStatement(queryModificacion);
            ps.setString(1, pelicula.getNombre());
            ps.setString(2, pelicula.getDescripcion());
            ps.setString(3, pelicula.getGenero());
            ps.setString(4, pelicula.getDirector());
            ps.setString(5, pelicula.getActorPrincipal());
            ps.setInt(6, pelicula.getDuracion());
            ps.setInt(7, pelicula.getId());
            ps.executeUpdate();
            
            ps.close();
            cn.close();
        } catch (SQLException e) {
        }
        return resultado;

    }

    public List<Pelicula> generaLista(String valor) throws SQLException, IOException {
            final String queryLista =("SELECT * FROM peliculas where CONCAT (GENRE_ID,'',NAME,'',DIRECTOR) LIKE '%"+valor+"%'");
            List listaPeliculas   = new ArrayList<Pelicula>();
            
            try{
            Connection cn = conexion.obtieneConexion();
            PreparedStatement st = cn.prepareStatement(queryLista);
            ResultSet rs = st.executeQuery();
            
            while (rs.next()) {
                Pelicula pelicula = new  Pelicula();
                pelicula.setId(rs.getInt(1));
                pelicula.setNombre(rs.getString(2));
                pelicula.setDescripcion(rs.getString(3));
                pelicula.setGenero(rs.getString(4));
                pelicula.setDirector(rs.getString(5));
                pelicula.setActorPrincipal(rs.getString(6));
                pelicula.setDuracion(rs.getInt(7));
                listaPeliculas.add(pelicula);
                
            }
        } catch (SQLException e){
            System.out.println("Error:"+ e.getMessage());
        }
        return listaPeliculas;
    }

    @Override
    public Integer idPelicula() {
        Integer id = 0;
        String sql = "select max(ID) + 1 as codigo from peliculas";
        
        try{
            Connection cn = conexion.obtieneConexion();
            PreparedStatement ps = cn.prepareStatement(sql);
            
            ResultSet rs = ps.executeQuery();
            
            if(rs.next()){
                id = rs.getInt(1);
            }
            
            ps.close();
            cn.close();
        }
        catch(Exception ex){
            System.out.println("Error:" + ex.getMessage());
        }
        
         return id;   
    }

    @Override
    public Pelicula get(Integer id) {
        Pelicula pelicula = null;
        
        String sql = "select * from peliculas where ID = ?";
        
        try{
            Connection cn = null;
            try {
                cn = conexion.obtieneConexion();
            } catch (IOException ex) {
                Logger.getLogger(PeliculaDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
            PreparedStatement ps = cn.prepareStatement(sql);
            
            ps.setInt(1, id);
            
            ResultSet  rs = ps.executeQuery();
            pelicula = new Pelicula();
            
            if(rs.next()){
                  pelicula.setId(rs.getInt(1));
                  pelicula.setNombre(rs.getString(2));
                  pelicula.setGenero(rs.getString(4));
                  pelicula.setDescripcion(rs.getString(3));
                  pelicula.setDirector(rs.getString(5));
                  pelicula.setActorPrincipal(rs.getString(6));
                  pelicula.setDuracion(rs.getInt(7));
            }
            cn.close();
            ps.close();
       }
       catch(SQLException ex){
                System.out.println("Error:"+ ex.getMessage());
       }

        return pelicula;
}
    
    
}